package com.apps.essentials;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.widget.Toast;

import com.apps.realtyportal.HomeActivity.Exchanger;
import com.google.android.maps.GeoPoint;

public class AddressGeocoder extends AsyncTask<String, Void, List<Address>>{
	private Context context;
	@Override
	protected List<Address> doInBackground(String... addressname) {
		// TODO Auto-generated method stub
		Geocoder geocoder = new Geocoder(context);
		List<Address> addresses = null;
		
		try {
			// Getting a maximum of 3 Address that matches the input text
			addresses = geocoder.getFromLocationName(addressname[0], 3);
		} catch (IOException e) {
			e.printStackTrace();
		}			
		return addresses;
	}
	
	@Override
	protected void onPostExecute(List<Address> addresses) {
		// TODO Auto-generated method stub
        if(addresses==null || addresses.size()==0){
			Toast.makeText(context, "No Location found", Toast.LENGTH_SHORT).show();
			
			// Redraws the map to clear the overlays
	        Exchanger.mMapView.invalidate();
		}
		for(int i=0;i<addresses.size();i++){				
			
			Address address = (Address) addresses.get(i);
			
	        // Creating an instance of GeoPoint, to display in Google Map
	        GeoPoint p = new GeoPoint(
	                                  (int)(address.getLatitude()*1E6),
	                                  (int)(address.getLongitude()*1E6)
	                                 );
	        
	        String addressText = String.format("%s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                    address.getCountryName());
	        
	        // Locate the first location
	        if(i==0){
	        	
	        }
	  
		}
		
		Exchanger.mMapView.invalidate();
	}
}
